﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using CastleDefender.Platform.ScreenBehavior;
using Microsoft.Xna.Framework.Graphics;
using CastleDefender.ConfigFolder;
using WindowsPhoneGame2;
using System.Data.Linq;
using System.Data.Linq.Mapping;

namespace CastleDefender.Platform.GameScene
{
    public partial class GameHighScore : GameInfo
    {

        #region VARIABLE
        
        private SpriteFont sfFont;
        private Vector2 vecPos;
        private Vector2 vecMeasure;
        private int intNewScore;
        private List<string> lstScores;
        private HighScoreContext data;

        #endregion

        #region CONSTRUCTOR

        public GameHighScore(Game myGame, ref TouchBehavior touch)
            : base(myGame, ref touch, "HighScore")
        {
            sfFont = myGame.Content.Load<SpriteFont>(Config.FolderFonts + "FontHeightScore");
            data = new HighScoreContext(Config.ConnectionString);
            lstScores = data.GetList();
            intNewScore = 0;
            vecMeasure = sfFont.MeasureString(lstScores[0]);
            vecPos = new Vector2((Config.ScreenSize.X - vecMeasure.X) / 2 + Config.InitPosXHighScore, Config.InitPosYHighScore);
        }

        #endregion

        #region PUBLIC METHOD

        public override void Show()
        {
            base.Show();
        }

        public override void Hide()
        {
            base.Hide();
        }

        public override void Draw(GameTime gameTime)
        {
            base.Draw(gameTime);

            for (int i = 0; i < lstScores.Count; i++)
            {
                Vector2 pos = vecPos + new Vector2(0, (vecMeasure.Y + 30) * i);
                sprBatch.DrawString(sfFont, lstScores[i], pos, Color.White);
            }

        }

        public void GetNewScore(int Score)
        {
            for (int i = 0; i < lstScores.Count; ++i)
            {
                //Duyệt trong list điểm cao nếu điểm mới cao hơn một trong các điểm có trong list
                if (int.Parse(lstScores[i]) <= Score)
                {
                    string strID = (i + 1).ToString();
                    try
                    {
                        var query = from score in data.HighScores where score.ID == strID select score;
                        foreach (HighScore score in query)
                        {
                            score.ID = strID;
                            score.Score = Score.ToString();
                        }
                        data.SubmitChanges();

                    }
                    catch { }
                    lstScores = data.GetList();
                    break;
                }
            }
        }

        #endregion

        #region PUBLIC PROPERTIES

        public int NewScore
        {
            get { return intNewScore; }
            set { intNewScore = value; }
        }
        
        #endregion

    }
}
